module game{
    /**
     * 格子代理
     */
    export class McCandy extends eui.Component{
        public voCandy:CandyVO;

        public txt_value:eui.Label;
        private _count:number;
        private _candy:egret.Shape;

        constructor(){
            super();
            // this.skinName = "resource/skin/cell.exml";
            this.width = 110;
            this.height =110;

             this._candy = new egret.Shape();
             this.addChild(this._candy);

             this.txt_value = new eui.Label();
             this.txt_value.textColor = 0x000000;
             this.txt_value.size = 30;
             this.addChild(this.txt_value);

        }

        public init(vo:CandyVO){
            this.voCandy = vo;
            this.setCandyValue(vo);
            this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClick,this);
        }

        public dispose(){
            if(this.parent){
                this.parent.removeChild(this);
            }
        }

        public destroy(){
            
        }

        private count:number = 0;
        public gameOver(){
            this.count = 0;
            this.tweenLeft();
            
        }

        private gaptime = 50;
        private ox = 0;
        private tweenLeft(){
            this.count++;
            this.ox = this.voCandy.x;
            if(this.count*this.gaptime == 1000){
                egret.Tween.removeTweens(this);
                egret.Tween.get(this).to({x:this.ox},this.gaptime);
                return;
            }
            egret.Tween.removeTweens(this);
            egret.Tween.get(this).to({x:this.ox-4},this.gaptime).call(this.tweenRight,this);
        }

        private tweenRight(){
            this.count++;
            if(this.count *this.gaptime == 1000){
                egret.Tween.removeTweens(this);
                egret.Tween.get(this).to({x:this.ox},this.gaptime);
                return;
            }
            egret.Tween.removeTweens(this);
            egret.Tween.get(this).to({x:this.ox+4},this.gaptime).call(this.tweenLeft,this);
        }


        private _color = [0xffffff,0XEEC024,0xAFD04E , 0xD47F49 ,0xCF4B49 ,0xD34997, 0x9C3FCE ,0x733BC6, 0x3340BE, 0x0C7EC4, 0x2DAFBA, 0x6FF1E1, 0xCBF4EF];
        
        private preColor:number;
        public setCandyValue(vo:CandyVO){
            let color ;
            if(vo.type == gameData.curmax) color = this._color[0];
            else color = this._color[vo.type%12+1];

            if(color == this.preColor){
                this.txt_value.text = vo.type+"";
                this.txt_value.x = this.width - this.txt_value.textWidth >> 1;
                this.txt_value.y = this.height - this.txt_value.textHeight >>1;
                return;
            }

            let g =this._candy.graphics;
            g.clear();
            g.beginFill(color);
            g.drawRoundRect(0,0,vo.width,vo.height,15);
            g.endFill();

            this.txt_value.text = vo.type+"";
            this.txt_value.x = this.width - this.txt_value.textWidth >> 1;
            this.txt_value.y = this.height - this.txt_value.textHeight >>1;
            this.preColor = color; 

        }

        private onClick(){
            App.ControllerManager.applyFunc(ControllerConst.Core,EventConst.Remove_Candy,this);
        }
        
    }
}